import 'package:fakegpt/page/chat/view.dart';
import 'package:fakegpt/page/home/logic.dart';
import 'package:fakegpt/util/Util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomePage extends GetView<HomeLogic> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(HomeLogic());
    final state = controller.state;
    return Scaffold(
      appBar: AppBar(
        title: const Text("Fake ChatGpt"),
        actions: [
          InkWell(
              onTap: () {},
              child: const Padding(
                padding: EdgeInsets.all(8.0),
                child: Icon(Icons.add),
              ))
        ],
      ),
      body: SafeArea(
          child: SmartRefresher(
        enablePullDown: true,
        onRefresh: controller.onRefresh,
        header: const WaterDropHeader(),
        controller: state.refreshController,
        child: SingleChildScrollView(
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.all(8.0),
                child: Text("声明：本App无需魔法，与您的ChatGpt官网数据同步"),
              ),
              GetBuilder<HomeLogic>(
                id: 'list',
                assignId: true,
                builder: (logic) {
                  return ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: state.list.length,
                      itemBuilder: (c, i) {
                        return Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 15, vertical: 10),
                          child: Container(
                            padding: const EdgeInsets.all(10),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius:
                                    const BorderRadius.all(Radius.circular(10)),
                                boxShadow: [
                                  BoxShadow(
                                      color: Colors.grey.withAlpha(30),
                                      blurRadius: 5,
                                      spreadRadius: 5)
                                ]),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                InkWell(
                                  onTap: () {
                                    controller.rename("${state.list[i].id}");
                                  },
                                  child: Row(
                                    children: [
                                      Text(
                                        "${state.list[i].title}",
                                        style: const TextStyle(
                                            fontWeight: FontWeight.bold),
                                      ),
                                      const Icon(
                                          Icons.drive_file_rename_outline)
                                    ],
                                  ),
                                ),
                                Padding(
                                  padding:
                                      const EdgeInsets.symmetric(vertical: 10),
                                  child: Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      const Text(
                                        "会话Id：",
                                        style: TextStyle(
                                            color: Colors.blue, fontSize: 13),
                                      ),
                                      Expanded(
                                        child: Text(
                                          "${state.list[i].id}",
                                          style: const TextStyle(
                                              color: Colors.blue, fontSize: 13),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Container(
                                      padding: const EdgeInsets.symmetric(
                                          horizontal: 15, vertical: 10),
                                      decoration: BoxDecoration(
                                          color: Colors.blue.shade200,
                                          borderRadius: const BorderRadius.all(
                                              Radius.circular(5))),
                                      child: Row(
                                        children: const [
                                          Icon(
                                            Icons.copy,
                                            size: 15,
                                            color: Colors.white,
                                          ),
                                          Text(
                                            "复制ID",
                                            style:
                                                TextStyle(color: Colors.white),
                                          ),
                                        ],
                                      ),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        controller.deleteConversation(
                                            state.list[i].id ?? "");
                                      },
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 15, vertical: 10),
                                        decoration: BoxDecoration(
                                            color: Colors.red.shade200,
                                            borderRadius:
                                                const BorderRadius.all(
                                                    Radius.circular(5))),
                                        child: Row(
                                          children: const [
                                            Icon(
                                              Icons.delete_forever_outlined,
                                              size: 20,
                                              color: Colors.white,
                                            ),
                                            Text(
                                              "删除会话",
                                              style: TextStyle(
                                                  color: Colors.white),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                    InkWell(
                                      onTap: () {
                                        Get.to(() => const ChatPage(),
                                            arguments: "${state.list[i].id}");
                                      },
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 15, vertical: 10),
                                        decoration: const BoxDecoration(
                                            color: Colors.blue,
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5))),
                                        child: Row(
                                          children: const [
                                            Icon(
                                              Icons.chat_outlined,
                                              size: 15,
                                              color: Colors.white,
                                            ),
                                            Text(
                                              "继续聊天",
                                              style: TextStyle(
                                                  color: Colors.white),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Text(
                                        Util.formatTime(DateTime.parse(
                                            "${state.list[i].updateTime}")),
                                        textAlign: TextAlign.right,
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      });
                },
              ),
            ],
          ),
        ),
      )),
    );
  }
}
